/*
 * This file is part of jboss-admin-rest
 * Copyright 2009, Christos Vasilakis (cvasilak@gmail.com)
 * 
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.admin.rest.util;

import org.jboss.admin.rest.domain.components.*;
import org.jboss.managed.api.ManagedComponent;
import org.jboss.managed.api.ManagedProperty;
import org.jboss.metatype.api.types.MetaType;
import org.jboss.metatype.api.values.EnumValue;
import org.jboss.metatype.api.values.SimpleValue;

import java.io.Serializable;

public class ManagedComponentUtils {

	public static ManagedComponentBean getWrappedComponentBean(ManagedComponent component) {
		ManagedComponentBean bean;

		String type = component.getType().getType();
		String subtype = component.getType().getSubtype();

		if (type.equals("DataSource")) {
			bean = new ManagedComponentDataSourceBean(component);
		} else if (type.equals("ConnectionFactory")) {
			bean = new ManagedComponentConnectionFactoryBean(component);
		} else if (type.equals("JMSDestination")) {
			bean = new ManagedComponentJMSDestinationBean(component);
		} else if (type.equals("MBean")) {

			if (subtype.equals("Servlet"))
				bean = new ManagedComponentServletBean(component);
			else
				bean = new ManagedComponentMBeanBean(component);
			
		} else if (type.equals("MCBean")) {
			bean = new ManagedComponentMCBeanBean(component);
		} else {
			bean = new ManagedComponentUnknownBean(component);
		}

		return bean;
	}

	public static String getProperty(String name, ManagedComponent component) {
		if (component == null) {
			throw new IllegalStateException("ManagedComponent is null!");
		}

		ManagedProperty property = component.getProperty(name);

		if (property == null) {
			throw new IllegalStateException("Property named '" + name + "' not found for ManagedComponent [" + component + "].");
		}

		return getProperty(property);
	}

	public static String getProperty(ManagedProperty property) {
		if (property == null) {
			throw new IllegalStateException("Property is null");
		}

		MetaType metaType = property.getMetaType();
		Serializable value;

		if (metaType.isSimple()) {
			SimpleValue simpleValue = (SimpleValue) property.getValue();
			value = (simpleValue != null) ? simpleValue.getValue() : null;
		} else if (metaType.isEnum()) {
			EnumValue enumValue = (EnumValue) property.getValue();
			value = (enumValue != null) ? enumValue.getValue() : null;
		} else if (metaType.isComposite()) {
			/*
			 * TODO 
			 * better handle composite types
			 */
			//CompositeMetaType type = (CompositeMetaType) metaType;
			value = property.getDescription();
		} else {
			//throw new IllegalStateException("Type of [" + property + "] is not simple or enum.");
			value = property.getDescription();
		}

		if (!metaType.getClassName().equals("java.lang.String")) {
			if (value != null) {
				return value.toString();  // convert to String for properties other than String
			} else {
				return "";
			}
		}

		return (String) value;
	}


	/* Retrieved from jopr-jboss5-plugin */
	public static Serializable getSimplePropertyValue(ManagedComponent component, String propertyName) {
		ManagedProperty property = component.getProperty(propertyName);
		if (property == null) {
			throw new IllegalStateException("Property named '" + propertyName + "' not found for ManagedComponent [" + component + "].");
		}
		MetaType metaType = property.getMetaType();
		Serializable value;
		if (metaType.isSimple()) {
			SimpleValue simpleValue = (SimpleValue) property.getValue();
			value = (simpleValue != null) ? simpleValue.getValue() : null;
		} else if (metaType.isEnum()) {
			EnumValue enumValue = (EnumValue) property.getValue();
			value = (enumValue != null) ? enumValue.getValue() : null;
		} else {
			throw new IllegalStateException("Type of [" + property + "] is not simple or enum.");
		}
		return value;
	}
}
